<?php

session_start();
include '../defines.php';
autentifikacijaAdmin();

$action = "";
$type = "";

if (!isset($_GET['action'])) myDie("", "index.php");
if (!isset($_GET['type'])) myDie("", "index.php");

$action = $_GET['action'];
$type = $_GET['type'];


if (strcmp($action,"new") == 0) {
	if (strcmp($type, "mis") == 0) {
		if (!isset($_POST['misVrsta'])) myDie("Informacije o mišu nisu upisane!","../misevi.php");
		
		$vrsta = $_POST['misVrsta'];
		$haplotip = $_POST['misHaplotip'];
		$soj = $_POST['misSoj'];
		$transgen = $_POST['misTransgen'];
		$nokgen = $_POST['misNokgen'];
		$info = $_POST['misInfo'];
				
		$values = array();
		$values[0][0] = "naziv"; $values[0][1] = $vrsta;
		$values[1][0] = "haplotip"; $values[1][1] = dbGetAtributeId("haplotip","naziv", $haplotip);		
		$values[2][0] = "soj"; $values[2][1] = dbGetAtributeId("soj","naziv", $soj);		
		$values[3][0] = "transgen"; $values[3][1] = dbGetAtributeId("transgen","naziv", $transgen);			
		$values[4][0] = "nokgen"; $values[4][1] = dbGetAtributeId("nokgen","naziv", $nokgen);		
		$values[5][0] = "info"; $values[5][1] = $info;	
		
		$misId = dbInsert("mis", $values, 6);
		header("Location: ../misevi.php?action=pretraga");
	}
	else if (strcmp($type, "haplotip") == 0) {
		if (!isset($_POST['haplotip'])) 
			myDie("Haplotip nije definiran!", "../misevi.php");
		
		$values = array();
		$values[0][0] = "naziv"; $values[0][1] = $_POST['haplotip'];
				
		dbInsert("haplotip", $values, 1);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "soj") == 0) {
		if (!isset($_POST['soj'])) 
			myDie("Naziv soja nije definiran!", "../misevi.php");
		
		$values = array();
		$values[0][0] = "naziv"; $values[0][1] = $_POST['soj'];
		
		dbInsert("soj", $values, 1);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "transgen") == 0) {
		if (!isset($_POST['transgen'])) 
			myDie("Naziv transgena nije definiran!", "../misevi.php");
		
		$values = array();
		$values[0][0] = "naziv"; $values[0][1] = $_POST['transgen'];
		dbInsert("transgen", $values, 1);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "nokgen") == 0) {
		if (!isset($_POST['nokgen'])) 
			myDie("Naziv nokautiranog gena nije definiran!", "../misevi.php");
		
		$values = array();
		$values[0][0] = "naziv"; $values[0][1] = $_POST['nokgen'];
		dbInsert("nokgen", $values, 1);
		header("Location: ../misevi.php?action=atributi");
	}
	else {
		
	}
}
else if (strcmp($action,"edit") == 0) {
	if (!isset($_POST['misVrsta'])) myDie("Informacije o mišu nisu upisane!","../misevi.php");
	if (!isset($_GET['id'])) myDie("Mis nije odabran!", "../misevi.php");
	
	$vrsta = $_POST['misVrsta'];
	$haplotip = $_POST['misHaplotip'];
	$soj = $_POST['misSoj'];
	$transgen = $_POST['misTransgen'];
	$nokgen = $_POST['misNokgen'];
	$info = $_POST['misInfo'];
	
	$values = array();
	$values[0][0] = "naziv"; $values[0][1] = $vrsta;
	$values[1][0] = "haplotip"; $values[1][1] = dbGetAtributeId("haplotip","naziv", $haplotip);
	$values[2][0] = "soj"; $values[2][1] = dbGetAtributeId("soj","naziv", $soj);
	$values[3][0] = "transgen"; $values[3][1] = dbGetAtributeId("transgen","naziv", $transgen);
	$values[4][0] = "nokgen"; $values[4][1] = dbGetAtributeId("nokgen","naziv", $nokgen);
	$values[5][0] = "info"; $values[5][1] = $info;
	
	dbUpdate("mis", $values, 6, $_GET['id']);
	header("Location: ../misevi.php?action=pretraga");
}
else if (strcmp($action,"delete") == 0) {
	if (strcmp($type, "mis") == 0) {
		if (!isset($_GET['id'])) myDie("Nije odabran miš za brisanje!", "misevi.php");

		$id = $_GET['id'];
		dbDelete("mis", $id);
		header("Location: ../misevi.php?action=pretraga");
	}
	else if (strcmp($type, "haplotip") == 0) {
		if (!isset($_GET['id'])) 
			myDie("Id haplotipa nije definiran!", "../misevi.php");

		$id = $_GET['id'];
		dbDelete("haplotip", $id);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "soj") == 0) {
		if (!isset($_GET['id'])) 
			myDie("Id soja nije definiran!", "../misevi.php");

		$id = $_GET['id'];
		dbDelete("soj", $id);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "transgen") == 0) {
		if (!isset($_GET['id'])) 
			myDie("Id transgena nije definiran!", "../misevi.php");

		$id = $_GET['id'];
		dbDelete("transgen", $id);
		header("Location: ../misevi.php?action=atributi");
	}
	else if (strcmp($type, "nokgen") == 0) {
		if (!isset($_GET['id'])) 
			myDie("Id nokautiranog gena nije definiran!", "../misevi.php");

		$id = $_GET['id'];
		dbDelete("nokgen", $id);
		header("Location: ../misevi.php?action=atributi");
	}
	else {
		
	}
}
else {
	
}